package com.wb.iec104.analysis;

import com.wb.iec104.factory.AnaFactory;
import com.wb.iec104.factory.DefaultAnaFactoryHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * 遥信信息解析
 *
 * @author
 */
@Slf4j
public class TelemetryIEC104 {
    private static AnaFactory anaFactory = new DefaultAnaFactoryHandler();


    /**
     * 不带时标的单双点信息（遥信）
     *
     * @param infoElement
     * @param num
     * @param tI
     * @param sQ
     * @return
     */
    public static void NoTime_Point(int[] infoElement, int num, int tI, int sQ) {
        Bean bean = null;
        if (sQ == 1) {
            int addr = ASDU.infoAddress(infoElement[0], infoElement[1], infoElement[2]);
            for (int i = 0; i < num; i++) {
                int qds = (infoElement[i + 3] & 0x01) == 1 ? 1 : 0;
                int tempAddr = addr + i;
                bean = new Bean(tempAddr, qds);
                anaFactory.handleData(bean);
            }

        } else {
            for (int i = 1; i <= num; i++) {
                int start = (i - 1) * 4;
                //信息题地址
                int addr = ASDU.infoAddress(infoElement[start], infoElement[start + 1], infoElement[start + 2]);
                int qds = (infoElement[start + 3] & 0x01) == 1 ? 1 : 0;
                bean = new Bean(addr, qds);
                anaFactory.handleData(bean);
            }

        }

    }

    private String point(int i, int tI) {
        StringBuilder builder = new StringBuilder();
        if (tI == 1 || tI == 30) {
            if ((i & 0x80) == 128) {
                builder.append("无效/");
            }
            if ((i & 0x80) == 0) {
                builder.append("有效/");
            }
            if ((i & 0x40) == 64) {
                builder.append("非当前值/");
            }
            if ((i & 0x40) == 0) {
                builder.append("当前值/");
            }
            if ((i & 0x20) == 32) {
                builder.append("被取代/");
            }
            if ((i & 0x20) == 0) {
                builder.append("未被取代/");
            }
            if ((i & 0x10) == 16) {
                builder.append("被闭锁/");
            }
            if ((i & 0x10) == 0) {
                builder.append("未被闭锁/");
            }
            if ((i & 0x01) == 1) {
                builder.append("开关合	");
            }
            if ((i & 0x01) == 0) {
                builder.append("开关分	");
            }
        }
        if (tI == 3 || tI == 31) {
            if ((i & 0x80) == 128) {
                builder.append("无效/");
            }
            if ((i & 0x80) == 0) {
                builder.append("有效/");
            }
            if ((i & 0x40) == 64) {
                builder.append("非当前值/");
            }
            if ((i & 0x40) == 0) {
                builder.append("当前值/");
            }
            if ((i & 0x20) == 32) {
                builder.append("被取代/");
            }
            if ((i & 0x20) == 0) {
                builder.append("未被取代/");
            }
            if ((i & 0x10) == 16) {
                builder.append("被闭锁/");
            }
            if ((i & 0x10) == 0) {
                builder.append("未被闭锁/");
            }
            if ((i & 0x03) == 0) {
                builder.append("不确定或中间状态	");
            }
            if ((i & 0x03) == 1) {
                builder.append("确定开关分	");
            }
            if ((i & 0x03) == 2) {
                builder.append("确定开关合	");
            }
            if ((i & 0x03) == 3) {
                builder.append("不确定	");
            }

        }

        return builder.toString();
    }

    /**
     * 带CP56Time2a时标的单双点信息（遥信）
     *
     * @param infoElement
     * @param num
     * @param tI
     * @param sQ
     * @return
     */
    public static void Time_Point(int[] infoElement, int num, int tI, int sQ) {
        //af 24 00 00 a8 61 52 10 7e 73 16 01 a8 61 52 10 7e 73 16 01
        if (sQ == 1) {
            int addr = ASDU.infoAddress(infoElement[0], infoElement[1], infoElement[2]);
            Bean bean = null;
            long timeMi = 0;
            for (int i = 0; i < num; i++) {
                int start = (i * 8) + 3;
                addr += i;
                int dqs = infoElement[start];
                int time[] = new int[7];

                for (int j = 1; j < 8; j++) {
                    time[j - 1] = infoElement[start + j];
                }


                bean = new Bean(addr, dqs);
                anaFactory.handleData(bean);
            }
        } else {
            //("按照DL/T 634.5101-2002规定，带长时标的单/双点信息遥信报文并不存在信息元素序列（SQ=1）的情况。");
        }
    }


}
